google.load("search", "1");

nextSearch = 0;

function OnLoad() {
	 // Add in local search
	if(document.getElementById('destcounts')){
		var totalcount = document.getElementById('destcounts').value;
		for(var i=1;i<=totalcount;i++){
			buildSearch();
		}
	}
}

function buildSearch()
{
	var localSearch = new google.search.LocalSearch();
	// Create a search control
	var searchControl = new google.search.SearchControl();	     	  
	var options = new google.search.SearcherOptions();
	var addre = "address"+(nextSearch*2);
	options.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);		
	
	localSearch.setCenterPoint(document.getElementById(addre).value);

	searchControl.addSearcher(localSearch, options);
		    	
	localSearch.setResultSetSize("5");  // set results page
	
	var searchcntl = 'searchcontrol'+(nextSearch*2);

	searchControl.draw(document.getElementById(searchcntl));

	searchControl.setSearchCompleteCallback(this, gotResults);
	
	searchControl.execute("");
	nextSearch ++;
}

function googlesearch(googleid)
{
	var googlelink = 'googleversion'+googleid;
	$('#'+googlelink).show();
	$('#suggestList li').show();
}

function gotResults(sc, searcher)
{
	var resultcontent = '';
	var results = 'searchresults'+searcher.Dq;
	console.log(searcher);
	var resultdiv = document.getElementById(results);	
	
	for (var i=0; i<searcher.results.length; i++)
	{
		var result = searcher.results[i];
		var link = "<a href='"+result.url+"'>"+result.title+"</a>";
		resultcontent += '<li><div class="addPoi lfloat" locationInfo="'+result.streetAddress+' '+result.city+' '+result.region+'"><img src="img/icons/add_16.png"></div><div class="suggestListItem"><p>'+link+'<br />'+result.streetAddress+'<br />'+result.city+','+result.region+'</p></div></li>';
	}	
	resultdiv.innerHTML = resultcontent;			    
}

google.setOnLoadCallback(OnLoad);

  